﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADNTimeline
{
    public partial class LayerItem : Control
    {
        public LayerItem()
        {
            InitializeComponent();
        }

        public LayerItem(string layerText, bool checkDisplay, bool checkLock)
        {
            InitializeComponent();

            if (layerText != null) { lblText.Text = layerText; }
            chkDisplayed.Checked = checkDisplay;
            chkLocked.Checked = checkLock;
        }

        public event EventHandler DisplayCheck;
        public event EventHandler LockCheck;
        public event EventHandler ItemSelected;

        private void OnDisplayCheck(bool isChecked)
        {
            LayerItemEventArgs e = new LayerItemEventArgs(isChecked);
            if (DisplayCheck != null) { DisplayCheck(this, e); }
        }

        private void OnLockCheck(bool isChecked)
        {
            LayerItemEventArgs e = new LayerItemEventArgs(isChecked);
            if (LockCheck != null) { LockCheck(this, e); }
        }

        private void OnItemSelected()
        {
			if (ItemSelected != null) { ItemSelected(this, EventArgs.Empty); }
        }

        public override string Text
        {
            get { return lblText.Text; }
            set { lblText.Text = value; }
        }

        public bool Displayed
        {
            get { return chkDisplayed.Checked; }
            set { chkDisplayed.Checked = value; }
        }

        public bool Locked
        {
            get { return chkLocked.Checked; }
            set { chkLocked.Checked = value; }
        }

        private void chkLocked_CheckedChanged(object sender, EventArgs e)
        {
            OnLockCheck(chkLocked.Checked);
        }

        private void chkDisplayed_CheckedChanged(object sender, EventArgs e)
        {
            OnDisplayCheck(chkDisplayed.Checked);
        }

        private void lblText_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            lblText.Visible = false;
            TextBox labelEdit = new TextBox();
            labelEdit.Width = lblText.Width;
            labelEdit.Text = lblText.Text;
			labelEdit.Name = "LabelEdit";
            labelEdit.KeyPress += new KeyPressEventHandler(labelEdit_KeyPress);
            labelEdit.Leave +=new EventHandler(labelEdit_Leave); 
            this.Controls.Add(labelEdit);
            labelEdit.Focus();
        }

        void labelEdit_Leave(object sender, EventArgs e)
        {
            Deselect();
        }

        void labelEdit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                Deselect();
                e.Handled = false;
            }
        }

		public void Deselect()
		{
			TextBox labelEdit = (TextBox)this.Controls["LabelEdit"];
			if(labelEdit != null)
			{
				lblText.Text = labelEdit.Text;
				this.Controls.Remove(labelEdit);
				lblText.Visible = true;
			}
		}

        private void LayerItem_MouseClick(object sender, MouseEventArgs e)
        {
            OnItemSelected();
        }
    }

    public class LayerItemEventArgs : EventArgs
    {
        public LayerItemEventArgs() { }
        public LayerItemEventArgs(bool isChecked)
        {
            IsChecked = isChecked;
        }

        public bool IsChecked { get; set; }
        public int ItemIndex { get; set; }
    }
}
